package com.example.labyrinth;

import java.util.List;

public class PathToCheeseFinder {

    String path = "";

    int posX = 1;
    int posY = 1;

    List<List<Character>> labyrinth;
    char platz;
    char schrit = ' ';

    public PathToCheeseFinder(List<List<Character>> labyrinth) {
        this.labyrinth = labyrinth;
    }

    public String findPathToCheese() {
        labyrinth.get(posY).get(posX);

        while (labyrinth.get(posY).get(posX) != 'C') { // runter

            boolean mazeSolverStep = mazeSolverStep('C',' ','.');
            if (mazeSolverStep == false) {

                boolean mazeSolverStepBack = mazeSolverStep('.', '.', 'X');
                if (!mazeSolverStepBack) {
                    break;
                }
            }
            /* 
            char oben = labyrinth.get(posY - 1).get(posX);
            char unten = labyrinth.get(posY + 1).get(posX);
            char rechts = labyrinth.get(posY).get(posX + 1);
            char links = labyrinth.get(posY).get(posX - 1);
            boolean rechtsFrei, linksFrei, obenFrei, untenFrei = true;
            platz = labyrinth.get(posY).get(posX);
            labyrinth.get(posY).set(posX, 'M');

            if (rechts == ' ' || rechts == 'C') {

                // if (platz == 'C'){
                labyrinth.get(posY).set(posX, 'M');
                labyrinth.get(posY).set(posX, '.');
                // break;}
                // labyrinth.get(posY).set(posX + 1, 'M');
                posX = posX + 1;
                path += 'R';
            } else {
                rechtsFrei = false;

                if (links == ' ' || links == 'C') {

                    labyrinth.get(posY).set(posX, 'M');
                    labyrinth.get(posY).set(posX, '.');

                    posX = posX - 1;
                    path += 'L';
                } else {

                    linksFrei = false;

                    if (oben == ' ' || oben == 'C') {

                        // if (platz == 'C'){
                        labyrinth.get(posY).set(posX, 'M');
                        labyrinth.get(posY).set(posX, '.');
                        // break;}
                        // labyrinth.get(posY - 1).set(posX, 'M');
                        posY = posY - 1;
                        path += 'O';
                    } else {
                        obenFrei = false;

                        if (unten == ' ' || unten == 'C') {

                            // if (platz == 'C'){

                            labyrinth.get(posY).set(posX, '.');
                            // break;}
                            // labyrinth.get(posY + 1).set(posX, 'M');
                            posY = posY + 1;

                            path += 'U';
                        } else {
                            labyrinth.get(posY).set(posX, 'M');
                            break;
                        }

                    }
                }
            } */

            // platz = labyrinth.get(posY).get(posX);

            // if (platz == 'C') { break;}
            // System.err.println("CHESSE FOUND!");
            // System.out.println(platz);
        }
        // rechts
        return path;

    }

    private boolean mazeSolverStep(char platz1, char platz2, char ersatz) {

        char oben = labyrinth.get(posY - 1).get(posX);
        char unten = labyrinth.get(posY + 1).get(posX);
        char rechts = labyrinth.get(posY).get(posX + 1);
        char links = labyrinth.get(posY).get(posX - 1);
        // boolean rechtsFrei, linksFrei, obenFrei, untenFrei = true;
        platz = labyrinth.get(posY).get(posX);
        labyrinth.get(posY).set(posX, 'M');

        if (rechts == platz1 || rechts == platz2) {

            // if (platz == 'C'){
            // labyrinth.get(posY).set(posX, 'M');
            labyrinth.get(posY).set(posX, ersatz);
            // break;}
            // labyrinth.get(posY).set(posX + 1, 'M');
            posX = posX + 1;
            path += 'R';
        } else {
            // rechtsFrei = false;

            if (links == platz1 || links == platz2) {

                // labyrinth.get(posY).set(posX, 'M');
                labyrinth.get(posY).set(posX, ersatz);

                posX = posX - 1;
                path += 'L';
            } else {

                // linksFrei = false;

                if (oben == platz1 || oben == platz2) {

                    // if (platz == 'C'){
                    // labyrinth.get(posY).set(posX, 'M');
                    labyrinth.get(posY).set(posX, ersatz);
                    // break;}
                    // labyrinth.get(posY - 1).set(posX, 'M');
                    posY = posY - 1;
                    path += 'O';
                } else {
                    // obenFrei = false;

                    if (unten == platz1 || unten == platz2) {

                        // if (platz == 'C'){

                        labyrinth.get(posY).set(posX, ersatz);
                        // break;}
                        // labyrinth.get(posY + 1).set(posX, 'M');
                        posY = posY + 1;

                        path += 'U';
                    } else {
                        // labyrinth.get(posY).set(posX, 'M');
                        return false;
                    }
                }
            }
        }

        return true;
    }

    private boolean isFree(List<List<Character>> labyrinth, int posYToTest, int posXToTest) {
        char character = labyrinth.get(posYToTest).get(posXToTest);

        return character == ' ' || character == 'C';
    }
}
